new Vue({
	el: '#app',
	// 选项
	data: {
		uid: '',
		aid: '',
		pid: '',
		isp: '0',
		comment: '',
		commentList: [],
		path: '',
		page: '1',
		t: [],
		isShow: false
	},
	mounted: function() {
		this.init();
	},
	methods: {
		init: function() {
			this.getMain();
		},
		//获得回答id和评论列表
		getMain: function() {
			var self = this;
			//self.getCommentList();
			self.uid = $api.getStorage('uid');
			apiready = function() {
				var fid = api.pageParam;
				self.aid = fid.aid;
				//loading
				self.isS();

				api.addEventListener({
					name: 'scrolltobottom',
					extra: {
						threshold: 0 //设置距离底部多少距离时触发，默认值为0，数字类型
					}
				}, function(ret, err) {
					//alert('已滚动到底部');
					self.page++;
					if(self.page > self.pageCount) {
						api.toast({
							msg: '没有更多了',
							duration: 1000,
							location: 'bottom'
						});
					} else {
						self.getCommentList();
					}
				});
			};
		},
		isS: function() {
			var self = this;
			var UILoading = api.require('UILoading');
			if(self.isShow) {
				console.log(self.loadingId);
				UILoading.closeFlower({
					id: self.loadingId
				});
			} else {
				UILoading.flower({
					center: {
						x: api.winWidth / 2.0,
						y: api.winHeight / 2.0
					},
					size: 30,
					fixed: true
				}, function(ret) {
					console.log(JSON.stringify(ret));
					self.loadingId = ret.id;
				});
				self.getCommentList();
			}
		},
		//评论列表
		getCommentList: function() {
			var self = this;
			console.log(self.aid);
			var data = {
				sns_answer_id: self.aid,
				page: self.page
			}
			$.post(base + config.sns_answer_comment_view, data, function(msg) {
				console.log(msg);
				self.t = msg.data;
				for(var i = 0; i < self.t.length; i++) {
					self.commentList.push(self.t[i]);
				};
				self.pageCount = msg.page_count;
				self.path = msg.url;
				self.isShow = true;
				self.isS();
			}, 'json');
		},
		//跳转到对话评论页
		jump: function(id) {
			api.openFrame({
				name: 'comment-dialogue' + id,
				url: 'commentDialogue.html',
				vScrollBarEnabled: false,
				rect: {
					x: 0,
					y: 0,
					w: 'auto',
					h: 'auto'
				},
				reload: true,
				pageParam: {
					id: id
				}
			});
		},
		//发布我的评论
		sendComment: function() {
			var self = this;
			if(self.uid == "" || self.uid == null) {
				api.confirm({
					title: '尚未登录',
					msg: '是否前往登录页面',
					buttons: ['确定', '取消']
				}, function(ret, err) {
					var index = ret.buttonIndex;
					if(index == 1) {
						self.layout();
					} else {

					}
				});
			} else {
				if(self.isp == 0) {
					var data = {
						sns_answer_id: self.aid,
						content: self.comment,
						user_id: self.uid
					}
				} else {
					var data = {
						sns_answer_id: self.aid,
						content: self.comment,
						user_id: self.uid,
						parent_id: self.pid
					}
				}
				$.post(base + config.sns_answer_comment_add, data, function(msg) {
					console.log(msg);
					if(msg.error == 0) {
						self.comment = "";
						if(self.isp == 0) {

						} else {
							self.remove();
						}
						self.page = "1";
						self.commentList = [];
						self.getCommentList();
					} else {
						console.log(msg.error_msg);
						api.toast({
							msg: msg.error_msg,
							duration: 2000,
							location: 'middle'
						});
					}
				}, 'json');
			}
		},
		//回复评论
		reply: function(pid, pname) {
			var self = this;
			if(self.uid) {
				self.comment = "";
				self.comment = "回复" + pname + "的评论：";
				self.pid = pid;
				self.isp = '1';
			} else {
				api.confirm({
					title: '尚未登录',
					msg: '是否前往登录页面',
					buttons: ['确定', '取消']
				}, function(ret, err) {
					var index = ret.buttonIndex;
					if(index == 1) {
						self.layout();
					} else {

					}
				});
			}
			console.log(this.aid, this.pid);
		},
		remove: function() {
			this.comment = "";
			this.isp = '0';
		},
		//登出
		layout: function() {
			var self = this;
			api.openWin({
				name: 'login',
				url: '../../html/login/login.html',
				reload: true,
				pageParam: {
					openName: 'reply-comment',
					aid: self.aid,
					type:1
				}
			});
		}
	}
})